package com.dm.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dm.shop.common.enums.ResultEnum;
import com.dm.shop.dto.AuthDTO;
import com.dm.shop.entity.Admin;
import com.dm.shop.entity.User;
import com.dm.shop.service.AdminService;
import com.dm.shop.service.UserService;
import com.dm.shop.utils.ResultUtil;
import com.dm.shop.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @version 1.0
 * @author: Saiya Wang
 * @desc: 用户的登录注册
 * @date: 2021/7/21 14:20
 */
@CrossOrigin
@RestController
@Slf4j
public class AuthController {
    @Autowired
    UserService userService;

    @Autowired
    AdminService adminService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

//    @Resource
//    PasswordEncoder passwordEncoder;

    @Value("${user.default.avatar}")
    private String avatar;

    /**
     * 用户登录  区分用户和管理员
     * @param authDTO
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody @Valid AuthDTO authDTO)
    {
        log.info("登录信息："+authDTO);
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("phone_number",authDTO.getUsername());
        User user = userService.getOne(userQueryWrapper);

        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("phone_number",authDTO.getUsername());
        Admin admin = adminService.getOne(adminQueryWrapper);
        if(user == null)
        {
            if(admin == null)
            {
                return ResultUtil.error(ResultEnum.LOGIN_FAILED);
            }
            //matches(原密码，加密后的密码) 明文，密文 如果匹配不成功false 取反返回失败
//            if(!passwordEncoder.matches(authDTO.getPassword(),admin.getPassword()))
            if (!authDTO.getPassword().equals(admin.getPassword()))
            {
                return ResultUtil.error(ResultEnum.LOGIN_FAILED);
            }
            return ResultUtil.success("登录成功",admin);
        }
//        if(!passwordEncoder.matches(authDTO.getPassword(),user.getPassword()))
        if (!authDTO.getPassword().equals(user.getPassword()))
        {
            return ResultUtil.error(ResultEnum.LOGIN_FAILED);
        }
        return ResultUtil.success("登录成功",user);
    }

    /**
     * 用户注册
     * @param authDTO
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody AuthDTO authDTO)
    {
        log.info("注册信息"+authDTO);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("nick_name",authDTO.getName());
        if(userService.getOne(wrapper) != null)
        {
            return ResultUtil.error("昵称已经被占用");
        }
        wrapper = new QueryWrapper<>();
        wrapper.eq("phone_number",authDTO.getPhone());
        if (userService.getOne(wrapper) != null){
            return ResultUtil.error("电话已被注册");
        }
        wrapper = new QueryWrapper<>();
        wrapper.eq("email",authDTO.getMail());
        if (userService.getOne(wrapper) != null)
        {
            return ResultUtil.error("邮箱已被注册");
        }
        User user = new User();
        user.setAvatar(avatar);//头像设为默认值
        user.setNickName(authDTO.getUsername());
        user.setEmail(authDTO.getMail());
        user.setPhoneNumber(authDTO.getPhone());
        user.setSex(authDTO.getSex());
        user.setUserName(authDTO.getName());
        user.setPassword(authDTO.getPassword());
        try {
//            userService.addUser(user);
            userService.save(user);
        }catch (Exception e){
            log.error(e.getMessage());
            return  ResultUtil.error("注册信息有误");
        }
        return ResultUtil.success("注册成功");
    }

    /**
     * 短信验证码
     * @param phone
     * @return
     */
//    @PostMapping("/verifyCode")
//    public Result verifyCode(@RequestBody String phone)
//    {
//
//    }
}
